<template>
  <div style="margin-top: 60px; margin-left: 80px;">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="80px"
      class="demo-ruleForm"
      style="display: flex; align-items: center; margin-bottom: 20px;"
    >
      <el-form-item label="字段：" prop="key" style="margin-right: 20px; margin-bottom: 0;">
        <el-select v-model="ruleForm.key" style="width: 160px;" placeholder="请选择字段">
          <el-option label="学号" value="number"></el-option>
          <el-option label="姓名" value="name"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="值：" style="margin-right: 20px; margin-bottom: 0;">
        <el-input v-model="ruleForm.value" placeholder="请输入关键字" style="width: 160px;"/>
      </el-form-item>

      <el-button
        type="primary"
        icon="el-icon-search"
        @click="submitForm('ruleForm')"
      >
        搜索
      </el-button>
    </el-form>

    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%;"
    >
      <el-table-column
        fixed
        prop="id"
        label="ID"
        width="130">
      </el-table-column>
      <el-table-column
        prop="dormitoryName"
        label="宿舍"
        width="130">
      </el-table-column>
      <el-table-column
        prop="number"
        label="学号"
        width="130">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="130">
      </el-table-column>
      <el-table-column
        prop="gender"
        label="性别"
        width="130">
      </el-table-column>
      <el-table-column
        prop="state"
        label="状态"
        width="130">
      </el-table-column>
      <el-table-column
        prop="createDate"
        label="入住时间"
        width="180">
      </el-table-column>
      <el-table-column label="操作" width="230">
        <template #default="scope">
          <el-button
            size="mini"
            type="danger"
            @click="moveout(scope.row)"
          >迁出</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination 
      style="margin-top: 20px;float: right"
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :total="total"
      :current-page.sync="currentPage"
      @current-change="pageChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import { moveoutApi } from '@/api/move'

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      ruleForm: {
        key: '',
        value: '',
        page: 1,
        size: 5
      },
      rules: {
        key: [{ required: true, message: '请选择字段', trigger: 'change' }],
        value: [{ required: true, message: '请输入关键字', trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return
        this.ruleForm.page = 1
        this.ruleForm.size = this.pageSize
        moveoutApi.search(this.ruleForm).then(resp => {
          this.tableData = resp.data.data
          this.total = resp.data.total
          this.currentPage = 1
        }).catch(() => {
          this.$message.error('搜索失败')
        })
      })
    },
    pageChange(page) {
      this.currentPage = page
      if (!this.ruleForm.value) {
        moveoutApi.getList(page, this.pageSize).then(resp => {
          this.tableData = resp.data.data
          this.total = resp.data.total
        })
      } else {
        this.ruleForm.page = page
        this.ruleForm.size = this.pageSize
        moveoutApi.search(this.ruleForm).then(resp => {
          this.tableData = resp.data.data
          this.total = resp.data.total
        })
      }
    },
    moveout(row) {
      this.$prompt('请输入迁出原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /.+/,
        inputErrorMessage: '迁出原因不能为空'
      }).then(({ value }) => {
        this.$confirm(`确认迁出【${row.name}】吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          moveoutApi.moveout(row.id, value).then(resp => {
            if (resp.code === 0) {
              this.$alert(`【${row.name}】已迁出`, '', {
                confirmButtonText: '确定',
                callback: () => this.pageChange(this.currentPage)
              })
            } else {
              this.$message.error('迁出失败')
            }
          })
        })
      })
    }
  },
  created() {
    this.pageChange(1)
  }
}
</script>